Using the plot functions

Introduction

The vignette Preparing your data for plotting shows how you can load your own data into a cropr_input object. This vignette shows how this object can then be used to create plots. Let’s load some example data objects:

library(CroPlotR)

workspace <- system.file(file.path("extdata", "example_input"), package = "CroPlotR")
soil_data <- readRDS(file.path(workspace, "soil_data_long.rds"))
weather_data <- readRDS(file.path(workspace, "weather_data_list.rds"))

soil <- set_soil(
  soil_data, 
  id = "name", 
  layer_thickness = list("epc", "cm"), 
  layer_water_field_cap = list("HCCF", "g/g"),
  layer_water_wilting_pt = list("HMINF", "g/g"), 
  layer_bulk_density = list("DAF", "g/cm^3"),
  organic_N_conc = list("norg", "g/g")
)

weather <- set_weather(
  weather_data,
  station_name = "station",
  temp_day_max = list("ttmax", "celsius"),
  temp_day_min = list("ttmin", "celsius"),
  year = "year"
)

The plot functions

CroPlotR provides different plot functions: plot_soil and plot_weather. All of these functions provide a number of plot types that mostly use data from the corresponding data object, but not exclusively. A list of all possible plots with examples for each plot function can be found in List of provided plots. Plot functions are used in a very straightforward way:

plot_soil(soil, type="thickness.mswc")

All the plot functions work in exaclty the same way. This vignette will use the plot_soil function as an example to illustrate the plot functions’ different functionalities.

The type = "all" option

The plot functions provide a type "all". When choosing this type, the plot function returns a list containing all plots that can be created from the given data. You can set the argument verbose = TRUE to see why missing plots have not been produced.

list_plots <- plot_soil(soil, type="all", verbose = TRUE)
#> ! Could not plot limiting.temperatures: Plot limiting_temperatures requires data object weather.
list_plots[["thickness.mswc"]]

Making plots interactive

Any plot can be transformed to an interactive plotly plot using the interactive argument.

plot_soil(soil, type="thickness.mswc", interactive = TRUE)

The special case of labeled scatter plots

There can sometimes be too little space on a labeled scatter plot to fit all the labels; this is the case of our example plot. The plot functions throw a warning in these situations (we decided not to show this warning in our vignettes). In these cases, you have three options:

  1. Ignore the warning and accept the missing labels.
  2. Make the plot interactive.
  3. Force more or all labels.

CroPlotR provides a special function for the third option that allows you to force more labels by replacing the plot’s ggrepel::geom_text_repel object (see ?ggrepel::geom_text_repel for more details):

library(ggrepel)
p <- plot_soil(soil, type="thickness.mswc")
geomTextRepel(p) <- geom_text_repel(max.overlaps = Inf)
p

Plots for large data sets

For large data sets, scatter plots quickly become unreadible and confusing. Thus, CroPlotR supports a histogram-like representation where the area of the plot is devided into hexagons that are then coloured according to the number of observations that fall therein (the following plot is interactive, try hovering with your mouse over the data points!).

plot_soil(soil, type="thickness.mswc", histogram = TRUE)

Whether or not this form of histogram-like representation is used can be specified using the histogram argument. By default, this form of representation is activated for data sets with more than 100 observations. Furthermore, plots in histogram-like representation are provided as interactive plotly plots. This behaviour can make it more difficult to customize the plot and it can be deactivated by setting the interactive argument to FALSE.

Graph customization

The plot functions allow you to customize the resulting plots in two ways. First of all, the plots are generated using the ggplot2 library and can hence be modified using the library’s + syntax. Secondly, they can be modified using the plot function’s ... argument.

Using ggplot’s + syntax

CroPlotR generates all plots using the library ggplot2 and they can hence be customized using the library’s + syntax. For instance, in order to change the axis labels or in order to add a line to the graph, one can simply write

library(ggplot2)
plot_soil(soil, type="thickness.mswc") + 
  # overwrite label on x axis
  xlab("My modified label on x axis") + 
  # add horizontal red line
  geom_abline(slope = 3.6, intercept = -90, color = "red")

Using the ... argument with constant values

Plots can also be customized using the plot function’s ... argument. All arguments given here are passed to the ggplot2 geometric function when creating the graph. On the present graph, one might for instance want to change the points’ size and opacity in order to make overplotting visible. To do so, one can simply write

library(ggplot2)
plot_soil(soil, type = "thickness.mswc", size = 2, alpha = 0.3)

Note that this is a special usage of the ... argument since the values of size and alpha are both constant, ie. they don’t make any reference to the data provided.

Using the ... argument with data variables

Even more customisations are possible when using the ... argument in combination with data variables. For instance, one might want to suppress the color scale of the above graph and replace it with a size scale, adding an approriate title to the newly created legend:

library(ggplot2)
plot_soil(soil, 
          type = "thickness.mswc", 
          # removing color scale that is usually present on this graph type
          colour = 1, 
          # adding our own size scale to the graph
          mapping = aes(size = organic_N_conc)) +
  # adding a label for our new size scale
  labs(size = "Organic N conc.")

The value assigned to the colour argument in this example is constant and it can hence be passed directly to the plot function as seen before The value of the colour aesthetic, on the other hand, is different for every observation and refers to a data variable—we want it to represent the concentration of organic nitrogen in each soil. Therefore, just as when creating a plot manually with ggplot2, this aesthetic has to be given through ggplot2’s aes function. Within the aes function, we can directly refer to data variable, in this case organic_N_conc. As shown in the example, a collection of (one ore more) aesthetics that is given through the aes function is named mapping within the ... argument.

The next example shows the true power of these customized aesthetics. Here, we want to identify graphically which soils in out data set have a particulary high concentration of organic nitrogen, let’s say more that 0.2 g/g. It is important to note that we have to give the bound of 0.2 together with a unit that is convertible to the unit of organic_N_conc.

library(ggplot2)
library(units)

plot_soil(
  soil, 
  type = "thickness.mswc", 
  mapping = aes(
    colour = ifelse(
      organic_N_conc >= set_units(0.2, "g/g"), 
      "\u2265 0.2 g/g", 
      "< 0.2 g/g"
    )
  )
)